// JavaScript Document
/**
*@description : 拖拽帮助类
@@version : 1.0
*@author : WangTao
*@date : 2013-10-31
*/

$(function(){
		//要拖动的element id
		var dragList=['autoPaint','filters','psBtns','line_size','shapes','direction'];
		var zIndex = 1;
		init();
		//初始化拖动事件
		function init(){
			for(var i=0,j=dragList.length;i<j;i++){
				addDragElements($("#"+dragList[i]).get(0));
			}  
		}
		
		//添加拖动事件
		function addDragElements (oDrag){
				var disX = dixY = 0;
				oDrag.onmousedown = function (event)
				{
					if(event.target.type=='range'){//如果点击的是range标签，不拖动
						return;
					}
					var event = event || window.event;
					disX = event.clientX - this.offsetLeft;
					disY = event.clientY - this.offsetTop;		
					
					var oTemp = document.createElement("div");
					oTemp["id"] = "temp";
					oTemp.style.left = this.currentStyle ? this.currentStyle["left"] : getComputedStyle(this, null)["left"];
					oTemp.style.top = this.currentStyle ? this.currentStyle["top"] : getComputedStyle(this, null)["top"];
					//oTemp.style.zIndex = zIndex++;
					document.body.appendChild(oTemp);
					
					document.onmousemove = function (event)
					{
						var event = event || window.event;
						var iL = event.clientX - disX;
						var iT = event.clientY - disY;
						var maxL = document.documentElement.clientWidth - oDrag.offsetWidth;
						var maxT = document.documentElement.clientHeight - oDrag.offsetHeight
						
						iL <= 0 && (iL = 0);
						iT <= 0 && (iT = 0);
						iL >= maxL && (iL = maxL);
						iT >= maxT && (iT = maxT);
						
						oTemp.style.left = iL + "px";
						oTemp.style.top = iT + "px";
						return false;
					};
					
					document.onmouseup = function ()
					{
						document.onmousemove = null;
						document.onmouseup = null;
						oDrag.style.left = oTemp.style.left;
						oDrag.style.top = oTemp.style.top;
						//oDrag.style.zIndex = oTemp.style.zIndex;
						document.body.removeChild(oTemp);
						oDrag.releaseCapture && oDrag.releaseCapture()
					};
					
					this.setCapture && this.setCapture();		
					return false
			}	
		}		  
});
